package org.pragmatic.paint.project;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;

public class MainFrame extends JFrame {
	public static String shapeType = "rectangle";
	public static Color shapeColor = Color.BLACK;
	public static Stroke strokeType = new BasicStroke(1);

	private Color color = Color.BLACK;

	// create MenuBar
	JMenuBar menuBar = new JMenuBar();
	JMenu menuFile = new JMenu("File");
	JMenuItem itemNewFile = new JMenuItem("New file", new ImageIcon("image/new-file-icon.png"));
	JMenuItem itemOpen = new JMenuItem("Open", new ImageIcon("image/document_open.png"));
	JMenuItem itemSave = new JMenuItem("Save", new ImageIcon("image/Save Icon.jpg"));
	JMenuItem itemExit = new JMenuItem("Exit", new ImageIcon("image/exit-icon.png"));
	

	// create ToolBar
	JToolBar toolBar = new JToolBar();
	JButton changeColorButton = new JButton(new ImageIcon("image/unnamed.png"));

	// create second ToolBar
	JToolBar leftBar = new JToolBar();
	JButton colorButton = new JButton(new ImageIcon("image/color.jpg"));
	JButton scaleButton = new JButton(new ImageIcon("image/scale.jpg"));
	JButton borderButton = new JButton(new ImageIcon("image/border.jpg"));
	
	public MainFrame(){
		//add MenuBar
		menuBar.add(menuFile);
		menuFile.add(itemNewFile);
		
		
		menuFile.add(itemOpen);
		menuFile.add(itemSave);
		menuFile.addSeparator();
	
		menuFile.add(itemExit);
	  
		class exitaction implements ActionListener {
			public void actionPerformed (ActionEvent e) {
				System.exit(0);
			}
		}
		itemExit.addActionListener(new exitaction());
		
			
		//add ToolBar
		JLabel labelShapes = new JLabel("Choose shape: ");
		String[] elements = {"Rectangle", "Oval", "Triangle"};
		final JComboBox<String> comboShapes = new JComboBox<String>(elements);
		toolBar.add(labelShapes);
		toolBar.add(comboShapes);
		toolBar.addSeparator();
		JLabel labelLines = new JLabel("Line: ");
		String[] lines = {"Thin line", "Thick line"};
		final JComboBox<String> comboLines = new JComboBox<String>(lines);
		toolBar.add(labelLines);
		toolBar.add(comboLines);
		toolBar.addSeparator();
		toolBar.add(changeColorButton);
		
		//add second ToolBar
		leftBar.add(colorButton);
		leftBar.add(scaleButton);
		leftBar.add(borderButton);
	
		this.setLayout(new BorderLayout());
		getContentPane().add(toolBar, BorderLayout.NORTH);
		getContentPane().add(new Canvas(), BorderLayout.CENTER);
		getContentPane().add(leftBar, BorderLayout.SOUTH);
		
		comboShapes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				switch (comboShapes.getSelectedIndex()) {
				case 0:
					shapeType = "rectangle";
					break;
				case 1:
					shapeType = "oval";
					break;
				case 2:
					shapeType = "triangle";
					break;

				default:
					shapeType = "rectangle";
					break;
				}
			}
		});
		
		changeColorButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				color = JColorChooser.showDialog(changeColorButton, "Choose a color", color);
				shapeColor = color;
				changeColorButton.setBackground(color);
			}
		});
		
		comboLines.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				switch(comboLines.getSelectedIndex()){
				case 0:
					strokeType = new BasicStroke(1);
					break;
				case 1:
					strokeType = new BasicStroke(4);
					break;
					
				default:
					strokeType = new BasicStroke(1);
					break;
				}
				
			}
		});
		
		this.setJMenuBar(menuBar);
		this.setTitle("Paint");
		this.setSize(800,600);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
	}

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		new MainFrame();

	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

}
